<?php
/**
 * @link https://www.kancloud.cn/cleverstone/ymb2
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\tab\util\titem;

use Yii;
use builder\base\ViewBuilderInterface;

/**
 * 项类型继承类
 * @author cleverstone
 * @since ym2.0
 */
abstract class TabItemType
{
    /**
     * @var string 项ID
     */
    protected $id;

    /**
     * @var ViewBuilderInterface|string 项内容
     */
    protected $content;

    /**
     * @var bool 是否选中当前项
     */
    protected $selected = false;

    /**
     * @var string js切换重载属性, 即: window.ym2BehaviorReqExport对象下的方法名
     */
    protected $reloadProperty = '';

    /**
     * @var int 计数器
     */
    protected static $counter = 0;

    /**
     * 实例构造
     * TabItemType constructor.
     * @param ViewBuilderInterface|string|callable $content 支持依赖注入。
     */
    public function __construct($content)
    {
        // 注册内容
        if (is_callable($content)) {
            $content = Yii::createObject($content);
        }

        $this->content = $content;

        $name = $this->name();
        // 初始化项ID
        $this->id = $name . self::$counter;

        self::$counter++;
        $this->init();
    }

    /**
     * 初始化
     */
    protected function init()
    {
    }

    /**
     * 项ID
     * @param string $id
     * @return $this
     */
    public function id($id)
    {
        $this->id = (string)$id;
        return $this;
    }

    /**
     * 设置选中当前项
     * @return $this
     */
    public function selected()
    {
        $this->selected = true;
        return $this;
    }

    /**
     * 定义项名称
     * @return string
     */
    abstract protected function name();

    /**
     * 输出数组配置
     * @return array
     */
    abstract public function _toArray();
}